package com.medusa.aps.business.modules.basic.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.input.SupplierDTO;
import com.medusa.aps.business.modules.basic.model.input.SupplierParam;
import com.medusa.aps.business.modules.basic.model.output.SupplierVO;
import com.medusa.aps.business.modules.basic.mp.entity.Supplier;
import com.medusa.aps.business.modules.basic.mp.service.SupplierService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:50
 */
@Validated
@RestController
@RequestMapping("/supplier")
@Tag(name = "供应商管理", description = "供应商管理")
public class SupplierController {
    /**
     * 服务对象
     */
    @Resource
    private SupplierService supplierService;

    /**
     * 分页查询所有数据
     *
     * @param supplierParam 分页参数
     * @return 所有数据
     */

    @GetMapping("/selectAll")
    @Operation(summary = "分页查询所有数据")
    public Result<Page<Supplier>> pageSupplier(SupplierParam supplierParam) {
        return Result.ok(supplierService.pageSupplier(supplierParam));
    }

    /**
     * 查询供应商详情
     *
     * @param id 供应商id
     */
    @GetMapping("/selectById")
    @Operation(summary = "查询供应商详情")
    public Result<SupplierVO> getSupplierDetail(@RequestParam("id") Long id) {
        return Result.ok(
                supplierService.getSupplierDetail(id)
        );
    }

    /**
     * 新增数据
     *
     * @param supplierDTO 实体对象
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据")
    public Result<Void> saveSupplier(@RequestBody @Valid SupplierDTO supplierDTO) {
        supplierService.saveSupplier(supplierDTO);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param supplier 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改数据")
    public Result<Void> updateSupplier(@RequestBody @Validated(Update.class) SupplierDTO supplier) {
        supplierService.updateSupplier(supplier);
        return Result.ok();
    }

    /**
     * 删除数据供应商物料
     *
     * @param supplierIds 供应商id集合
     * @param materialId  物料id
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据供应商物料")
    public Result<Void> deleteSupplier(@RequestParam(required = false) List<Long> supplierIds,
                                       @RequestParam(required = false) Long materialId) {
        supplierService.deleteSupplier(supplierIds, materialId);
        return Result.ok();
    }
}

